<?php


require_once LIB_PATH . 'Parser.php';

class PhpFile extends PhpElement
{
	/**
	 * indexed 
	 * 
	 * @var boolean
	 * @access private
	 */
	private $indexed = FALSE;

	/**
	 * elements 
	 * 
	 * @var array|NULL
	 * @access private
	 */
	private $elements;
	
	/**
	 * __construct
	 * 
	 * @access public
	 * @author Pablo López Torres <pablolopeztorres@gmail.com> 
	 * @param mixed $filename 
	 * @param mixed $autoIndex 
	 * @return void
	 */
	public function __construct($filename, Project $project, $autoIndex = FALSE)
	{
		parent::__construct($filename, $project);

		$this->path = $project->getPath() . $filename;
	
		if ($autoIndex)
		{
			$this->index();
		}
	}

	public function getPersistentKey()
	{
		return $this->getProject()->getName() . '/file_' . preg_replace('/[^a-z]/i', '_', $this->getName());
	}

	public function index()
	{
		$tokens = Tokenizer::prepare(file_get_contents($this->path));

		$this->elements = Parser::parseFileFromSource($tokens);
		$this->indexed = TRUE;

/*
        // load the catalog for the required project
        $catalogKey = $this->name . '/catalog';

        $catalog = Storage::load($catalogKey);

        if ($catalog === NULL)
        {
            $catalog = array(
                'methods' => array(),
                'classes' => array(),
            );
        }


		$elements = array();
		do 
		{
			$token =& current($tokens);

			$element = Parser::fetchUnknownElement($tokens, $this->path . $filename, $this->name);

			if (isset($element))
			{
				$elements[] = $element;

				if ($element instanceof PhpClass)
				{
					$persistentKey = $element->getPersistentKey();

					// save it!!
					if (Storage::save($element, $persistentKey, $forceWrite))
					{
						print "Class '{$element->getName()}' has been added to the index.\n";

						$catalog['classes'][] = $element->getName();
					}
				}
			}
		}
		while (next($tokens));

        // saving catalog with the class names
        Storage::save($catalog, $catalogKey, TRUE);

		$this->filenames[$filename] = TRUE;
		ProjectFactory::getInstance()->save($this->name, TRUE); // override the previous one
*/
	}

	/**
	 * Checks if the file is already indexed or not.
	 * 
	 * @access public
	 * @author Pablo López Torres <pablolopeztorres@gmail.com> 
	 * @return void
	 */
	public function isIndexed() {

	}
}

